<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>客服中心</title>
    <script src="/apps/js/baidu-tongji-mtx.js"></script>
    <link rel="stylesheet" href="/apps/css/bootstrap.min.css">
    <link href="/apps/css/fontawesome-free-6.4.0-web/css/all.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #2EA6E0;
            --primary-color-rgb: 46, 166, 224;
            --background-color: #F5FAFF;
            --title-color: #2EA6E0;
            --subtitle-color: #5D6D7E;
            --tab-default-color: #34495E;
            --tab-active-color: #FFFFFF;
            --card-bg-color: #F3E9D8;
            --modal-bg-color: #F9F5EF;
            --modal-border-color: #E0DED8;
        }

        body {
            font-family: "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif;
            background-color: var(--background-color);
            background-image: url('https://totemlife.cn/apps/public/images/demo/bg-helpdesk.jpg');
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
            transition: background-image 0.5s ease;
            line-height: 1.5;
        }

        body::before {
            display: none;
        }

        .page-header {
            background: transparent;
            text-align: center;
            padding: 2.2rem 1rem 1.2rem 1rem;
            margin-bottom: 0.5rem;
        }

        .page-header-title {
            font-size: 1.5rem;
            font-weight: 700;
            color: var(--title-color);
            letter-spacing: 4px;
            margin-bottom: 0.5rem;
            line-height: 1.4;
        }

        .page-header-desc {
            font-size: 0.875rem;
            color: var(--subtitle-color);
            letter-spacing: 0.2em;
            margin-bottom: 0.2rem;
            font-weight: 400;
        }

        .page-header-slogan {
            font-size: 0.875rem;
            color: var(--subtitle-color);
            letter-spacing: 0.3em;
            margin-bottom: 0.2rem;
            font-weight: 400;
        }

        .helpdesk-section {
            margin-top: 1.5rem;
        }

        .helpdesk-group-tabs {
            background: transparent;
            box-shadow: none;
            border-radius: 0;
            padding: 0 0 0.5rem 0;
            margin-bottom: 2rem;
            overflow-x: auto;
            white-space: nowrap;
            display: flex;
            align-items: center;
            gap: 1.2rem;
        }

        .helpdesk-group-tabs .btn {
            background: transparent !important;
            color: var(--tab-default-color) !important;
            font-weight: 500;
            border: none;
            margin: 0 0.5rem 0 0;
            border-radius: 22px;
            font-size: 0.9375rem;
            padding: 0.7rem 2.2rem;
            position: relative;
            transition: all 0.2s ease;
            display: inline-block;
        }

        .helpdesk-group-tabs .btn.active,
        .helpdesk-group-tabs .btn:active,
        .helpdesk-group-tabs .btn:focus {
            background: var(--primary-color) !important;
            color: var(--tab-active-color) !important;
            box-shadow: 0 4px 16px rgba(46, 166, 224, 0.15);
        }

        .helpdesk-group-tabs .btn:hover:not(.active) {
            color: var(--primary-color) !important;
            background: rgba(46, 166, 224, 0.1) !important;
        }

        .helpdesk-card {
            background: var(--card-bg-color);
            border-radius: 24px;
            box-shadow: 0 2px 8px rgba(46, 166, 224, 0.08);
            margin-bottom: 2rem;
            padding: 1.7rem 1.5rem;
            border: none;
            display: flex;
            align-items: center;
            position: relative;
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }

        .helpdesk-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(46, 166, 224, 0.12);
        }

        .helpdesk-avatar {
            width: 72px;
            height: 72px;
            border-radius: 50%;
            object-fit: cover;
            margin-right: 1.5rem;
            border: 3px solid var(--primary-color);
            background: #fff;
        }

        .helpdesk-info {
            flex: 1;
        }

        .helpdesk-title {
            font-size: 0.9375rem;
            font-weight: 600;
            color: var(--primary-color);
            margin-bottom: 0.2rem;
        }

        .helpdesk-desc {
            color: var(--subtitle-color);
            font-size: 0.8125rem;
            line-height: 1.6;
            font-weight: 400;
        }

        .helpdesk-phone {
            display: block;
            background: var(--primary-color);
            color: #FFFFFF;
            border-radius: 24px;
            padding: 0.7rem 0;
            font-size: 1.1rem;
            margin: 1.2rem auto 1rem auto;
            text-align: center;
            font-weight: 700;
            letter-spacing: 1px;
            box-shadow: 0 2px 8px rgba(46, 166, 224, 0.15);
            width: 220px;
            max-width: 90%;
            text-decoration: none;
            transition: background-color 0.2s ease;
        }

        .helpdesk-phone:hover {
            background: #218bb6;
            color: #FFFFFF;
        }

        @media (max-width: 767.98px) {
            .page-header-title {
                font-size: 1.25rem;
            }

            .page-header-desc,
            .page-header-slogan {
                font-size: 0.8125rem;
            }

            .helpdesk-title {
                font-size: 0.875rem;
            }

            .helpdesk-desc {
                font-size: 0.75rem;
            }

            .helpdesk-card {
                border-radius: 16px;
                padding: 1rem 0.5rem;
            }

            .helpdesk-avatar {
                margin-right: 1rem;
                width: 56px;
                height: 56px;
            }

            .helpdesk-group-tabs {
                border-radius: 0;
            }

            .contact-btn {
                right: 1rem;
                font-size: 0.8125rem;
                padding: 0.5rem 1.2rem;
            }

            .helpdesk-phone {
                font-size: 0.95rem;
                padding: 0.6rem 0;
                width: 90%;
                min-width: 160px;
                max-width: 220px;
            }
        }

        .contact-btn {
            position: absolute;
            right: 2rem;
            top: 50%;
            transform: translateY(-50%);
            background: var(--primary-color);
            color: #FFFFFF;
            border: none;
            border-radius: 50px;
            padding: 0.7rem 1.7rem;
            font-size: 0.9375rem;
            font-weight: 600;
            box-shadow: 0 2px 8px rgba(46, 166, 224, 0.15);
            display: flex;
            align-items: center;
            gap: 0.5rem;
            cursor: pointer;
            transition: all 0.2s ease;
        }

        .contact-btn:hover {
            background: #218bb6;
            color: #FFFFFF;
            box-shadow: 0 4px 12px rgba(46, 166, 224, 0.2);
        }

        @media (max-width: 767.98px) {
            .contact-btn {
                right: 1rem;
                font-size: 0.8125rem;
                padding: 0.5rem 1.2rem;
            }
        }

        .main-card {
            background: rgba(46, 166, 224, 0.618);
            border-radius: 32px;
            max-width: 96%;
            margin: 0.5rem auto 0.5rem auto;
            box-shadow: 0 8px 32px rgba(46, 166, 224, 0.1);
            padding: 1.2rem 0.2rem 0.2rem 0.2rem;
            position: relative;
        }

        /* 弹窗样式优化 */
        .modal-content {
            background-color: var(--modal-bg-color);
            border: none;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            transition: all 0.2s ease-in-out;
        }

        .modal.fade .modal-dialog {
            transform: scale(0.95);
            transition: transform 0.2s ease-in-out;
        }

        .modal.show .modal-dialog {
            transform: scale(1);
        }

        .modal-header {
            border-bottom: 1px solid var(--modal-border-color);
            padding: 1.2rem 1.5rem;
        }

        .modal-title {
            color: var(--title-color);
            font-size: 1.125rem;
            font-weight: 600;
            letter-spacing: 0.5px;
        }

        .modal-body {
            padding: 1.5rem;
        }

        .modal .btn-close {
            color: var(--subtitle-color);
            opacity: 0.8;
            transition: all 0.2s ease;
            padding: 0.5rem;
            margin: -0.5rem;
            border-radius: 50%;
        }

        .modal .btn-close:hover {
            background-color: rgba(46, 166, 224, 0.05);
            opacity: 1;
        }

        .modal .btn-close:focus {
            box-shadow: none;
        }

        .qrcode-container {
            background: #F6F1E7;
            border: 1px solid var(--modal-border-color);
            border-radius: 8px;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
            text-align: center;
            box-shadow: 0 2px 8px rgba(46, 166, 224, 0.04);
        }

        .qrcode-container img {
            max-width: 200px;
            height: auto;
            margin: 0 auto;
        }

        .modal-tip {
            color: var(--subtitle-color);
            font-size: 0.8125rem;
            line-height: 1.6;
            text-align: center;
            margin: 0;
        }

        .modal .btn-success {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
            padding: 0.6rem 1.5rem;
            font-size: 0.9375rem;
            font-weight: 500;
            border-radius: 8px;
            transition: all 0.2s ease;
        }

        .modal .btn-success:hover {
            background-color: #218bb6;
            border-color: #218bb6;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(46, 166, 224, 0.15);
        }

        @media (max-width: 767.98px) {
            .modal-title {
                font-size: 1rem;
            }

            .qrcode-container {
                padding: 1rem;
            }

            .qrcode-container img {
                max-width: 160px;
            }

            .modal-tip {
                font-size: 0.75rem;
            }
        }
    </style>
</head>

<body>
    <div class="page-header">
        <div class="page-header-title" id="merchantName">客服中心</div>
        <div class="page-header-desc">爱与科学同行，奇迹不再遥远</div>
        <div class="page-header-slogan">— 专业·用心·守望 —</div>
    </div>
    <div class="main-card" style="margin-top:0.3rem;">
        <div class="container helpdesk-section" style="margin-top:0;">
            <div class="helpdesk-group-tabs mb-1" id="helpdeskGroupTabs"></div>
            <div id="helpdeskGroups"></div>
        </div>
        <a class="helpdesk-phone" id="helpdeskPhone" href="tel:" style="text-decoration:none;"><i
                class="fas fa-phone-alt"></i> 4008001234</a>
    </div>
    <!-- 客服弹窗modal -->
    <div class="modal fade" id="helpdeskModal" tabindex="-1" aria-labelledby="helpdeskModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="helpdeskModalLabel">客服二维码</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body" id="helpdeskModalBody">
                    <!-- 二维码和说明 -->
                </div>
            </div>
        </div>
    </div>

    <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js" defer></script>
    <script src="/apps/js/bootstrap.bundle.min.js" defer></script>
    <script src="/apps/js/float-home-button.js" defer></script>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const merId = '{{mer_id}}';
            let groups = [];

            // 初始化悬浮按钮
            initFloatButton();

            fetch(`/apps/data/helpdesk/${merId}`)
                .then(res => res.json())
                .then(result => {
                    if (result.code === 0 && result.data) {
                        renderHelpdesk(result.data);
                    } else {
                        document.getElementById('merchantName').textContent = '客服中心';
                        document.getElementById('helpdeskGroups').innerHTML = '<div class="text-center text-danger">未找到客服数据</div>';
                    }
                })
                .catch(() => {
                    document.getElementById('merchantName').textContent = '客服中心';
                    document.getElementById('helpdeskGroups').innerHTML = '<div class="text-center text-danger">客服数据加载失败</div>';
                });

            function renderHelpdesk(data) {
                document.getElementById('merchantName').textContent = data.merchant_name || '客服中心';
                var phone = data.phone ? data.phone.replace(/[^\d]/g, '') : '';
                var phoneText = data.phone ? data.phone : '';
                var phoneHref = phone ? `tel:${phone}` : '';
                var phoneBtn = document.getElementById('helpdeskPhone');
                phoneBtn.innerHTML = `<i class="fas fa-phone-alt"></i> ${phoneText}`;
                phoneBtn.setAttribute('href', phoneHref);
                groups = data.groups || [];
                renderGroupTabs();
                renderGroupCards(0);
            }

            function renderGroupTabs() {
                const tabsHtml = groups.map((group, idx) => `
                    <button class="btn${idx === 0 ? ' active' : ''}" data-idx="${idx}" type="button">${group.group_name}</button>
                `).join('');
                document.getElementById('helpdeskGroupTabs').innerHTML = tabsHtml;
                document.querySelectorAll('#helpdeskGroupTabs button').forEach(btn => {
                    btn.addEventListener('click', function () {
                        document.querySelectorAll('#helpdeskGroupTabs button').forEach(b => b.classList.remove('active'));
                        this.classList.add('active');
                        renderGroupCards(Number(this.getAttribute('data-idx')));
                    });
                });
            }

            function renderGroupCards(idx) {
                const group = groups[idx];
                if (!group) {
                    document.getElementById('helpdeskGroups').innerHTML = '';
                    return;
                }
                const cards = (group.services || []).map(service => `
                    <div class="helpdesk-card" data-service='${JSON.stringify(service)}'>
                        <img class="helpdesk-avatar" src="${service.avatar}" alt="${service.title}">
                        <div class="helpdesk-info">
                            <div class="helpdesk-title">${service.title}</div>
                            <div class="helpdesk-desc">${service.desc}</div>
                        </div>
                        <button class="contact-btn" type="button" data-service='${JSON.stringify(service)}'>
                            <i class="fas fa-comment-dots"></i> 联系
                        </button>
                    </div>
                `).join('');
                document.getElementById('helpdeskGroups').innerHTML = cards;
                document.querySelectorAll('.contact-btn').forEach(btn => {
                    btn.addEventListener('click', function (e) {
                        e.stopPropagation();
                        const service = JSON.parse(this.getAttribute('data-service'));
                        showHelpdeskModal(service);
                    });
                });
            }

            function showHelpdeskModal(service) {
                let modalBody = '';
                if (service.qrcode) {
                    modalBody += `
                        <div class="qrcode-container">
                            <img src="${service.qrcode}" alt="二维码" class="img-fluid">
                        </div>`;
                }
                if (service.link && service.link.trim()) {
                    modalBody += `
                        <div class="text-center">
                            <a href="${service.link}" target="_blank" class="btn btn-success">
                                ${service.tip || '点击进入在线对话'}
                            </a>
                        </div>`;
                } else if (service.tip) {
                    modalBody += `<p class="modal-tip">${service.tip}</p>`;
                }
                document.getElementById('helpdeskModalBody').innerHTML = modalBody;
                const modal = new bootstrap.Modal(document.getElementById('helpdeskModal'));
                modal.show();
            }
        });
    </script>
</body>

</html>
